package com.rome.hotel.data.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.rome.hotel.common.annotation.Anonymous;
import io.jsonwebtoken.lang.Assert;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.rome.hotel.common.annotation.Log;
import com.rome.hotel.common.core.controller.BaseController;
import com.rome.hotel.common.core.domain.AjaxResult;
import com.rome.hotel.common.enums.BusinessType;
import com.rome.hotel.data.domain.HotelUser;
import com.rome.hotel.data.service.IHotelUserService;
import com.rome.hotel.common.utils.poi.ExcelUtil;
import com.rome.hotel.common.core.page.TableDataInfo;

/**
 * 酒店会员Controller
 *
 * @author rome
 * @date 2023-12-06
 */
@RestController
@Anonymous
@RequestMapping("/data/user")
public class HotelUserController extends BaseController
{
    @Autowired
    private IHotelUserService hotelUserService;

    /**
     * 查询酒店会员列表
     */
    @PreAuthorize("@ss.hasPermi('data:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(HotelUser hotelUser)
    {
        startPage();
        List<HotelUser> list = hotelUserService.selectHotelUserList(hotelUser);
        return getDataTable(list);
    }

    /**
     * 导出酒店会员列表
     */
    @PreAuthorize("@ss.hasPermi('data:user:export')")
    @Log(title = "酒店会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HotelUser hotelUser)
    {
        List<HotelUser> list = hotelUserService.selectHotelUserList(hotelUser);
        ExcelUtil<HotelUser> util = new ExcelUtil<HotelUser>(HotelUser.class);
        util.exportExcel(response, list, "酒店会员数据");
    }

    /**
     * 获取酒店会员详细信息
     */
//    @PreAuthorize("@ss.hasPermi('data:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(hotelUserService.selectHotelUserByUserId(userId));
    }

    /**
     * 新增酒店会员
     */
//    @PreAuthorize("@ss.hasPermi('data:user:add')")
    @Log(title = "酒店会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HotelUser hotelUser)
    {
        return toAjax(hotelUserService.insertHotelUser(hotelUser));
    }

    /**
     * 修改酒店会员
     */
//    @PreAuthorize("@ss.hasPermi('data:user:edit')")
    @Log(title = "酒店会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HotelUser hotelUser)
    {
        return toAjax(hotelUserService.updateHotelUser(hotelUser));
    }

    /**
     * 删除酒店会员
     */
    @PreAuthorize("@ss.hasPermi('data:user:remove')")
    @Log(title = "酒店会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(hotelUserService.deleteHotelUserByUserIds(userIds));
    }

    /**
     * 根据账号查询酒店会员
     * @param hotelUser
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('data:user:queryByUserName')")
    @PostMapping("/queryByUserName")
    public AjaxResult queryByUserName(@RequestBody HotelUser hotelUser)
    {
        Assert.notNull(hotelUser,"参数异常，请联系管理员");
        Assert.notNull(hotelUser.getUsername(),"账号不能为空");
        Assert.notNull(hotelUser.getPassword(),"密码不能为空");
        HotelUser hotelUserQuery = hotelUserService.selectHotelUserByUsername(hotelUser.getUsername());
        if(null==hotelUserQuery|| !hotelUserQuery.getPassword().equals(hotelUser.getPassword())){
            return AjaxResult.error("账号或密码错误");
        }else {
            return AjaxResult.success("登录成功",hotelUserQuery);
        }
    }
}
